package com.leo.blog.web.ping;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.leo.blog.annotatiton.SystemLog;
import com.leo.blog.dto.PingExecution;
import com.leo.blog.entity.Bloger;
import com.leo.blog.entity.Ping;
import com.leo.blog.enums.PingStateEnum;
import com.leo.blog.service.PingService;
import com.leo.blog.util.ConstantUtil;
import com.leo.blog.util.HttpServletRequestUtil;

@Controller
@RequestMapping(value = "/ping")
public class PingManagementController {
	@Autowired
	private PingService pingService;
	
	@RequestMapping(value = "/getpinglistbyblogid")
	@ResponseBody
	public Map<String, Object> getPingListByBlogId(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的页码
		int pageIndex = HttpServletRequestUtil.getInt(request, "pageIndex");
		//获取前台传过来的每页要求返回的文章数上限
		int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
		
		//获取前台传过来的值
		int blogId = HttpServletRequestUtil.getInt(request, "blogId");
		int type = HttpServletRequestUtil.getInt(request, "type");
		String startTime = HttpServletRequestUtil.getString(request, "startTime");
		String endTime = HttpServletRequestUtil.getString(request, "endTime");
		
		Ping pingCondition = new Ping();
		pingCondition.setBlogId(blogId);
		pingCondition.setType(type);
		pingCondition.setStartTime(startTime);
		pingCondition.setEndTime(endTime);
		
		PingExecution pe = pingService.getPingListByBlogId(pingCondition, pageIndex, pageSize);
		if(pe.getState() == PingStateEnum.SUCCESS.getState()) {
			//返回页数
			int page = 0;
			int pageCount = pe.getCount();
			if(pageCount > pageSize) {
				if(pageCount%pageSize > 0) {
					page = pageCount/pageSize + 1;
				}else {
					page = pageCount/pageSize;
				}
				
			}else {
				page = 1;
			}
			
			modelMap.put("pingList", pe.getPingList());
			modelMap.put("count", pe.getCount());
			modelMap.put("page", page);
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "查询列表失败");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/deletepingbypingid")
	@ResponseBody
	public Map<String, Object> deletePingByPingId(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int pingId = HttpServletRequestUtil.getInt(request, "pingId");
		
		Bloger bloger = (Bloger)request.getSession().getAttribute("bloger");
		
		if(bloger.getHasPermission() == 1) {
			if(pingId > 0) {
				PingExecution pe = pingService.deletePingByPingId("删除评价", pingId);
				if(pe.getState() == PingStateEnum.SUCCESS.getState()) {
					modelMap.put("success", true);
				}else {
					modelMap.put("success", false);
					modelMap.put("msg", "删除失败");
				}
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "删除失败");
			}
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "无权限操作");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/addping")
	@ResponseBody
	@Transactional
	public Map<String, Object> addPing(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int blogId = HttpServletRequestUtil.getInt(request, "blogId");
		String pingName = HttpServletRequestUtil.getString(request, "pingName");
		String pingContext = HttpServletRequestUtil.getString(request, "pingContext");
		int type = HttpServletRequestUtil.getInt(request, "type");
		
		Ping ping = new Ping();
		
		ping.setBlogId(blogId);
		ping.setPingName(pingName);
		ping.setPingContext(pingContext);
		ping.setType(type);
		
		PingExecution pe = pingService.addPing("发表了评价", ping);
		if(pe.getState() == PingStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "添加失败");
		}
		
		return modelMap;
	}
}
